package com.zeng.sim.config;

import cn.hutool.core.thread.ThreadFactoryBuilder;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.zeng.sim.util.SysConfigUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 描述信息
 *
 * @author zengwanlin
 * @date 2023-12-22T11:57
 */
@Configuration(proxyBeanMethods = false)
public class SimulatorConfig {

    @Bean
    public RestTemplate restTemplate() {
        SimpleClientHttpRequestFactory httpRequestFactory = new SimpleClientHttpRequestFactory();
        httpRequestFactory.setConnectTimeout((int) TimeUnit.SECONDS.toMillis(10));
        httpRequestFactory.setReadTimeout((int) TimeUnit.SECONDS.toMillis(60));

        RestTemplate restTemplate = new RestTemplate();
        restTemplate.setRequestFactory(httpRequestFactory);
        return restTemplate;
    }

    @Bean
    public ThreadPoolExecutor busiPoolExecutor(ApplicationContext context) {
        String corePoolSizeStr = SysConfigUtil.getConfigValue(SysConfigUtil.CONFIG_TYPE_SYSTEM,
                SysConfigUtil.CONFIG_CODE_SIM_BUSI_POOL_CORE_SIZE, context);
        int corePoolSize = Integer.parseInt(corePoolSizeStr);
        if (corePoolSize <= 0) {
            corePoolSize = Runtime.getRuntime().availableProcessors();
        }

        ThreadFactory threadFactory = ThreadFactoryBuilder.create()
            .setNamePrefix("sim-busi-")
            .setUncaughtExceptionHandler((t, th) -> {
                Logger logger = LoggerFactory.getLogger(t.getClass());
                logger.error(th.getMessage(), th);
            })
            .setDaemon(false)
            .build();
        return new ThreadPoolExecutor(corePoolSize, corePoolSize * 2, 10, TimeUnit.MINUTES,
                new LinkedBlockingQueue<>(100), threadFactory, new ThreadPoolExecutor.AbortPolicy());
    }

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

        // 分页插件
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.SQLITE);
        paginationInnerInterceptor.setMaxLimit(500L);
        paginationInnerInterceptor.setOptimizeJoin(Boolean.TRUE);
        paginationInnerInterceptor.setOverflow(Boolean.FALSE);

        interceptor.addInnerInterceptor(paginationInnerInterceptor);
        return interceptor;
    }

}
